import Login from '../pages/login.js';
import Router from './director.js';
import Homepage from '../pages/homepage.js';
import Infos from '../pages/infoses.js';
import Film from '../pages/film.js';
import Cinemas from '../pages/cinemas.js';  //与主界面连接
import FilmAndCinemas from '../pages/filmAndCinemas.js';
import Soon from '../pages/soon.js';
import Show from '../pages/show.js';
import Play from '../pages/play.js';
import UserManage from '../pages/userManage.js';

const { $ } = layui;
let routes = {
    // '/homepage':() => new Homepage(),
    '/login': () => new Login(),
    '/homepage': {
        '/infoses': () => new Infos(),
        '/film': () => new Film(),
        '/filmAndCinemas': () => new FilmAndCinemas(),
        '/cinemas': () => new Cinemas(), //与主界面连接
        '/soon': () => new Soon(),
        '/show': () => new Show(),
        '/play': () => new Play(),
        '/userManage':()=> new UserManage(),
        on: (next) => new Homepage(next)

    }
}

// 设置ajax的全局属性，用于提交token信息
$.ajaxSettings.beforeSend = function (xhr) {
        // 在这里加上你的 token 
        xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.token);
    }
$.ajaxSettings.error = function (err) {
        if (err.status == 401) {
            location.href = "#login";
        }
    }

// 默认访问登录模块
location.hash = location.hash || "#login";
    let router = Router(routes).configure({
        recurse: 'forward', // 递归匹配forward、backword、false
        async: true,
        strict: false
    }); // 实例化路由
    router.init(); // 初始化路由